package com.example.firstapp.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.firstapp.R;
import com.example.firstapp.manager.IItemHelper;
import com.qmuiteam.qmui.widget.QMUIFontFitTextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MoveAndSwipedAdapter extends RecyclerView.Adapter<MoveAndSwipedAdapter.ViewHolder> implements IItemHelper {

    private Context context;
    private LayoutInflater mInflater = null;
    private List<String> mDatas = new ArrayList<>();

    public MoveAndSwipedAdapter(Context context) {
        this.context = context;
        mInflater = LayoutInflater.from(this.context);
    }

    public void addData(List<String> data) {
        mDatas.addAll(data);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new ViewHolder(mInflater.inflate(R.layout.activity_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.text.setText(mDatas.get(i));
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    /**
     * 移动
     *
     * @param oldPosition:移动前位置
     * @param newPosition:移动后位置
     */
    @Override
    public void itemMoved(int oldPosition, int newPosition) {
        Collections.swap(mDatas, oldPosition, newPosition);
        notifyItemMoved(oldPosition, newPosition);
    }

    /**
     * 销毁
     *
     * @param position
     */
    @Override
    public void itemDismiss(int position) {
        notifyItemRemoved(position);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        QMUIFontFitTextView text;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            text = itemView.findViewById(R.id.text);
        }
    }
}
